#include "ModuleConvert.h"

void ModuleConvert::init(QCVModuleHandlerInterface * parent)
{
	this->parent=parent;
	win=new QWidget();
  layout=new QVBoxLayout(win);

	cb_format=new QComboBox(win);
	cb_format->addItem(tr("..."));
	cb_format->addItem(tr("C3 CV_RGB2GRAY C1"));
	cb_format->addItem(tr("C1 CV_GRAY2RGB C3"));

	cb_format->addItem(tr("C3 CV_RGB2BGR C3"));
	
	cb_format->addItem(tr("C3 CV_YUV2RGB C3"));
	cb_format->addItem(tr("C3 CV_RGB2YUV C3"));

	cb_format->addItem(tr("C3 CV_RGB2HSV C3"));
	cb_format->addItem(tr("C3 CV_HSV2RGB C3"));

	layout->addWidget(cb_format);
}
////////////////////////////////////////////////////////////////////////////////
void ModuleConvert::free()
{
	delete win;
}
////////////////////////////////////////////////////////////////////////////////
cv::Mat & ModuleConvert::process()
{
	if(cb_format->currentIndex()==0)
	{
		return image;
	}

	//C3 CV_RGB2GRAY C1
	else if(cb_format->currentIndex()==1)
	{
		if(image.channels()==3)
		{
			cv::cvtColor(image,result,CV_RGB2GRAY,1);
		}
		else
		{
			std::cout<<"Erreur ModuleConvert: image.channels()!=3 impossible de convertir ( C3 CV_RGB2GRAY C1 )\n";
			result.release();
		}
	}

	//C1 CV_GRAY2RGB C3
	else if (cb_format->currentIndex()==2)
	{
		if(image.channels()==1)
		{
			cv::cvtColor(image,result,CV_GRAY2RGB,3);
		}
		else
		{
			std::cout<<"Erreur ModuleConvert: image.channels()!=1 impossible de convertir (C1 CV_GRAY2RGB C3)\n";	
			result.release();
		}
	}

	//C3 CV_RGB2BGR C3
	else if (cb_format->currentIndex()==3)
	{
		if(image.channels()==3)
		{
			cv::cvtColor(image,result,CV_RGB2BGR,3);
		}
		else
		{
			std::cout<<"Erreur ModuleConvert: image.channels()!=3 impossible de convertir ( C3 CV_RGB2BGR C3 )\n";	
			result.release();
		}
	}

	//C3 CV_YUV2RGB C3
	else if (cb_format->currentIndex()==4)
	{
		if(image.channels()==3)
		{
			cv::cvtColor(image,result,CV_YUV2RGB,3);
		}
		else
		{
			std::cout<<"Erreur ModuleConvert: image.channels()!=3 impossible de convertir ( C3 CV_YUV2RGB C3 )\n";	
			result.release();
		}
	}

	//C3 CV_RGB2YUV C3
	else if (cb_format->currentIndex()==5)
	{
		if(image.channels()==3)
		{
			cv::cvtColor(image,result,CV_RGB2YUV,3);
		}
		else
		{
			std::cout<<"Erreur ModuleConvert: image.channels()!=3 impossible de convertir ( C3 CV_RGB2YUV C3 )\n";	
			result.release();
		}
	}

	//C3 CV_RGB2HSV C3
	else if (cb_format->currentIndex()==6)
	{
		if(image.channels()==3)
		{
			cv::cvtColor(image,result,CV_RGB2HSV,3);
		}
		else
		{
			std::cout<<"Erreur ModuleConvert: image.channels()!=3 impossible de convertir ( C3 CV_RGB2HSV C3 )\n";	
			result.release();
		}
	}
	
	//C3 CV_HSV2RGB C3
	else if (cb_format->currentIndex()==7)
	{
		if(image.channels()==3)
		{
			cv::cvtColor(image,result,CV_HSV2RGB,3);
		}
		else
		{
			std::cout<<"Erreur ModuleConvert: image.channels()!=3 impossible de convertir ( C3 CV_HSV2RGB C3 )\n";	
			result.release();
		}
	}

	return result;
}
////////////////////////////////////////////////////////////////////////////////
void ModuleConvert::setInput(int n,const cv::Mat & m)
{
	if(n==0)
	{	
		image=m.clone();
	}
}
////////////////////////////////////////////////////////////////////////////////
QWidget* ModuleConvert::widget()
{
	return win;
}
////////////////////////////////////////////////////////////////////////////////
QString ModuleConvert::getName()
{
	return tr("Convertir");
}
////////////////////////////////////////////////////////////////////////////////
int ModuleConvert::nbInputs()
{
	return 1;
}
////////////////////////////////////////////////////////////////////////////////
bool ModuleConvert::setParams(QString params)
{
	QStringList list_params=params.split(";");
		
	if(list_params.size()==1)
	{
		cb_format->setCurrentIndex(list_params[0].toInt());
		return true;
	}
	else
	{
		return false;
	}	
}
////////////////////////////////////////////////////////////////////////////////
QString ModuleConvert::getParams()
{
	return QString::number(cb_format->currentIndex());
}

////////////////////////////////////////////////////////////////////////////////
QCVModuleInterface * ModuleConvert::clone()
{
	return new ModuleConvert;
}
////////////////////////////////////////////////////////////////////////////////
QString ModuleConvert::help(int id)
{
	if(id==-2)
	{
		return tr("Permet de X");
	}
	else if(id==-1)
	{
		return tr("image");
	}
	else if(id==0)
	{
		return tr("image");
	}
	else
	{
		return tr("erreur");
	}
}
////////////////////////////////////////////////////////////////////////////////
Q_EXPORT_PLUGIN2(ModuleConvert_plugin,ModuleConvert);  
